package cn.tedu.shoot;

import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Hero extends FlyingObject{
	public static final int BIGR = 3;
	private boolean up=false;
	private boolean down=false;
	private boolean left=false;
	private boolean right=false;
	private int speed;
	private int doubleFire;
	public int judge;
	private int addtime;
	public Hero(int heroNum){
		super(64,64,208,325);
		if(heroNum==1){
			this.width = 115;
			this.x = World.WIDTH/2-this.width/2;
			this.y = 325-this.height/2;
		}
		if(heroNum==2){
			this.width = 83;
			this.x = World.WIDTH/2-this.width/2;
			this.y = 315;
		}
		life = 3;
		doubleFire = 0;
		speed = 5;
		judge = 5;
	}
	public void moveTo(int x,int y) {
		this.x = x-this.width/2;
		this.y = y-this.height/2;
	}
	/**
	 * 增加方向
	 * @param e
	 */
	public int times1 = 0;
	public int times2 = 0;;
	public void addDirection(KeyEvent e){
		switch(e.getKeyCode()){
		case KeyEvent.VK_W:
			up=true;
			break;
		case KeyEvent.VK_UP:
			up=true;
			break;
		case KeyEvent.VK_S:
			down=true;
			break;
		case KeyEvent.VK_DOWN:
			down=true;
			break;
		case KeyEvent.VK_LEFT:
			left=true;
			break;
		case KeyEvent.VK_A:
			left=true;
			break;
		case KeyEvent.VK_RIGHT:
			right=true;
			break;
		case KeyEvent.VK_D:
			right=true;
			break;
		case KeyEvent.VK_ENTER:
			 if(World.state == World.GAME_OVER){
				World.state = World.START;
			}else if(World.state == World.RUNNING){
				World.state = World.PAUSE;
			}else{
				this.x=208;
				this.y=600;
				World.state = World.RUNNING;
			}
			break;
		case KeyEvent.VK_SPACE:
			if(World.state==World.RUNNING){
				if(times1<judge){//能放大招的次数
					times1++;
					World.state=World.GAME_INSIDE;
				}
			    
			}
			break;
		case KeyEvent.VK_R:
			if(World.state==World.RUNNING&&this.state!=DEAD){
				if(times2<5){//能放防护罩的次数
					times2++;
					World.num = 1;
				addtime+=500;
				this.state = BIGR;
				}
			}
			break;
		}
	}
	/**
	 * 取消某键
	 * @param e
	 */
	public void minusDirection(KeyEvent e){
		switch(e.getKeyCode()){
		case KeyEvent.VK_W:
			up=false;
			break;
		case KeyEvent.VK_UP:
			up=false;
			break;
		case KeyEvent.VK_S:
			down=false;
			break;
		case KeyEvent.VK_DOWN:
			down=false;
			break;
		case KeyEvent.VK_A:
			left=false;
			break;
		case KeyEvent.VK_LEFT:
			left=false;
			break;
		case KeyEvent.VK_D:
			right=false;
			break;
		case KeyEvent.VK_RIGHT:
			right=false;
			break;
		}
	}
	public void move() {
		if(this.isLife()||this.isBigR()){
			if(up&&y>=0){
				y-=speed;
			}
			if(down&&y<=World.HEIGHT-2*this.height){
				y+=speed;
			}
			if(right&&x<=World.WIDTH-this.width){
				x+=speed;
			}
			if(left&&x>=0){
				x-=speed;
			}
		}
	}
	public void step() {
			y--;
	}
	int index1 = 0,index2 = 0;//下标
	int b = 2;
	double c = 0;
	int time = 1;//防护罩时间
	public BufferedImage getImage() {
		if(isLife()) {
			if(World.heroNum==1){
				return Images.hero1;
			}else if(World.heroNum==2){
				return Images.hero2;
			}
			return Images.hero[index1++%2];
		}else if(isBigR()){
			if(time++%addtime==0){
				this.state = LIFE;
			}
			if(World.heroNum==1){
				return Images.heros1;
			}else if(World.heroNum==2){
				return Images.heros2;
			}
			return Images.heros[index2++%2];
		}else if(isDead()) {	
			BufferedImage img = Images.hero[b];
			if(c++%40==0){
			b++;
			if(b==Images.hero.length) {
				state = REMOVE;
				//System.out.println("goudai");
			}
		  }
			return img;
		}
		
		return null;
	}
	public ArrayList<Bullet> shoot(int doubleFire) {//子弹发射,简化子弹火力
		int sum;   
		if(doubleFire<20){
			doubleFire=0;
			sum = 3;
		}else if(doubleFire<60){
			sum = 2;
		}else if(doubleFire<100){
			sum = 3;
		}else if(doubleFire<150){
			sum = 3;
		}else if(doubleFire<200){
			sum = 5;
		}else if(doubleFire<250){
			sum = 5;
		}else{
			sum = 7;
		}
		cutDoubleFire();
		if(sum%2!=0){
		//System.out.println(doubleFire);
		Bullet[] bs = new Bullet[sum];
		Bullet bullet = new Bullet(10,20);
		ArrayList<Bullet> c = new ArrayList<>();
		int m = bullet.width/2;
		int yStep = 20;
		for(int i=0;i<bs.length;i++){
			bs[i] = new Bullet(this.x+this.width/2-m,this.y-yStep);
				if(i==sum/2||sum==1){
					bs[i].xspeed=0;
					}else if(i<sum/2){
							bs[i].xspeed*=-(i+1);
				    }else{
							bs[i].xspeed*=(sum-i);
				    }
				c.add(bs[i]);
		}
		return c;
		}else{
		int xStep = this.width/(sum+1);//平行子弹
		int yStep = 20;
			Bullet[] bs = new Bullet[sum];
			Bullet bullet = new Bullet(10,20);
			int m = bullet.width/2;
			ArrayList<Bullet> b = new ArrayList<>();
			for(int i=0;i<bs.length;i++){
				bs[i] = new Bullet(this.x+(i+1)*xStep-m,this.y-yStep);
				bs[i].xspeed=0;
				b.add(bs[i]);
			}
			return b;
		}
		/*int xStep = this.width/4;
		int yStep = 20;
		if(doubleFire==0) {
			Bullet[] bs = new Bullet[1];
			bs[0] = new Bullet(this.x+2*xStep,this.y-yStep);
			return bs;
			
		}else if(doubleFire<30){
			Bullet[] bs = new Bullet[2];
			bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
			bs[1] = new Bullet(this.x+3*xStep,this.y-yStep);
			doubleFire-=2;
			return bs;
			
		}else{
			Bullet[] bs = new Bullet[3];
			bs[0] = new Bullet(this.x+1*xStep,this.y-yStep);
			bs[1] = new Bullet(this.x+2*xStep,this.y-yStep);
			bs[2] = new Bullet(this.x+3*xStep,this.y-yStep);
			doubleFire-=3;
			return bs;
		}*/
	}
	public void addLife(int num) {
		life+=num;
	}
	
	public void addDoubleFire(int num) {
		doubleFire+=num;
	}
	public int getDoubleFire(){
		return doubleFire;
	}
	public void cleanDoubleFire() {
		doubleFire -= 20;
	}
	public void cutDoubleFire(){
		doubleFire -= doubleFire/25;
	}
	public boolean isBigR(){
		return this.state == BIGR;
	}
}
